package com.neymar.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.neymar.entity.Applicant;
import com.neymar.entity.User;
import com.neymar.mapper.ApplicantMapper;
import com.neymar.service.ApplicantService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.rmi.MarshalledObject;
import java.sql.Wrapper;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 求职者表 服务实现类
 * </p>
 *
 * @author neymar
 * @since 2021-06-02
 */
@Service
public class ApplicantServiceImpl implements ApplicantService {

    @Autowired
    private ApplicantMapper applicantMapper;

    @Override
    public int save(Applicant applicant) {
        return applicantMapper.insert(applicant);
    }

    @Override
    public int deleteApplicantById(String userId) {
        return applicantMapper.deleteById(userId);
    }

    @Override
    public int updateApplicantById(Applicant applicant) {
        return applicantMapper.updateById(applicant);
    }

    @Override
    public Applicant findApplicantById(String userId) {
        QueryWrapper wrapper = new QueryWrapper<User>();
        wrapper.eq("user_id", userId);
        return applicantMapper.selectOne(wrapper);
    }

    @Override
    public List showStatusInfo() {
        QueryWrapper<Applicant> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("applicant_status", "count(*) as number").groupBy("applicant_status");
        List<Map<String, Object>> map = applicantMapper.selectMaps(queryWrapper);
        return map;
    }

    // https://www.jb51.net/article/196909.htm 取出某些字段的值
    // https://blog.csdn.net/weixin_44841849/article/details/105256360 使用分组函数
    @Override
    public List showGenderInfo() {
        QueryWrapper<Applicant> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("applicant_gender", "count(*) as number").groupBy("applicant_gender");
        List<Map<String, Object>> maps = applicantMapper.selectMaps(queryWrapper);
        return maps;
    }

    @Override
    public List showEduInfo() {
        QueryWrapper<Applicant> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("applicant_education", "count(*) as number").groupBy("applicant_education");
        List<Map<String, Object>> map = applicantMapper.selectMaps(queryWrapper);
        return map;
    }
}
